﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TvViewer.MpTvService;

namespace TvViewer
{
  public partial class ServerStatusForm : Form
  {
    ServiceInterface m_server;
    public ServerStatusForm(ServiceInterface _server)
    {
      InitializeComponent();
      m_server = _server;
    }

    private void ServerStatusForm_Load(object sender, EventArgs e)
    {
      timerRefreshStatus.Start();
    }

    private void FillListView(WebTvServerStatus[] states)
    {
      lvServerStates.Items.Clear();
      foreach (WebTvServerStatus s in states)
      {
        ListViewItem item = new ListViewItem(s.idCard.ToString());
        item.SubItems.Add(s.cardName);
        item.SubItems.Add(s.cardTypeStr);
        item.SubItems.Add(s.statusStr);
        item.SubItems.Add(s.userName);
        item.Tag = s;
        lvServerStates.Items.Add(item);
      }
    }

    private void timerRefreshStatus_Tick(object sender, EventArgs e)
    {
      try
      {
        WebTvServerStatus[] states = m_server.GetTvServerStatus();
        if (lvServerStates.Items.Count != states.Length)
        {
          FillListView(states);
        }
        else
        {
          foreach (WebTvServerStatus s in states)
          {
            bool removed = UpdateOrAdd(s);
            if (removed)
            {//the combination card/user doesn't exist anymore -> change card to new user or remove user
              ChangeUserOfCard(s);
            }
          }
        }
      }
      catch (Exception ex)
      {
        Console.WriteLine("Error retrieving server states, maybe connection down");
      }

    }

    private void ChangeUserOfCard(WebTvServerStatus _status)
    {
      foreach (ListViewItem i in lvServerStates.Items)
      {
        WebTvServerStatus s = i.Tag as WebTvServerStatus;
        if (s.idCard == _status.idCard)
        {
          i.SubItems[3].Text = _status.statusStr;
          i.SubItems[4].Text = _status.userName;
          i.Tag = _status;
        }
      }
    }

    private bool UpdateOrAdd(WebTvServerStatus _status)
    {
      //update cards
      foreach (ListViewItem i in lvServerStates.Items)
      {
        WebTvServerStatus s = i.Tag as WebTvServerStatus;
        if (s.idCard == _status.idCard && s.userName.Equals(_status.userName))
        {
          if (s.status != _status.status)
          {
            i.SubItems[3].Text = _status.statusStr;
          }
          return false;
        }
      }
      return true;
    }

    private void ServerStatusForm_FormClosing(object sender, FormClosingEventArgs e)
    {
      timerRefreshStatus.Stop();
    }

    private void cmServerStatus_Opening(object sender, CancelEventArgs e)
    {
      if (lvServerStates.SelectedItems != null && lvServerStates.SelectedItems.Count == 1)
      {
        WebTvServerStatus s = lvServerStates.SelectedItems[0].Tag as WebTvServerStatus;

        if (s.status == 1)
        {
          stopTimeshiftingToolStripMenuItem.Enabled = true;
        }
        else
        {
          stopTimeshiftingToolStripMenuItem.Enabled = false;
        }
      }

    }

    private void stopTimeshiftingToolStripMenuItem_Click(object sender, EventArgs e)
    {
      try
      {
        if (lvServerStates.SelectedItems != null && lvServerStates.SelectedItems.Count == 1)
        {
          WebTvServerStatus s = lvServerStates.SelectedItems[0].Tag as WebTvServerStatus;

          if (s.status == 1)
          {
            if (!m_server.StopTimeShifting(s.idChannel, s.idCard, s.userName))
            {
              MessageBox.Show("Couldn't stop timeshifting");
            }
          }
        }
      }
      catch (Exception ex)
      {
        MessageBox.Show("Error on stop timeshifting");
      }
    }
  }
}
